package com.tuanzi.loan.business.repository.commuser.impl;

import com.tuanzi.loan.business.entity.commuser.CommuserInfo;
import com.tuanzi.loan.business.repository.commuser.custom.CommuserInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.commuser.CommuserInfosearchRequest;
import com.tuanzi.loan.vo.response.commuser.CommuserInfoSearchResponse;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommUser.COMM_USER;

public class CommuserInfoRepositoryImpl extends JOOQBaseRepository implements CommuserInfoRepositoryCustom {

    @Override
    public Page<CommuserInfoSearchResponse> list(CommuserInfosearchRequest request, Pageable pageable) {
        fields(COMM_USER.ID,
                COMM_USER.MOBNUM,
                COMM_USER.WEIBO,COMM_USER.QQ,COMM_USER.WECHAT,COMM_USER.XIAOMI,COMM_USER.WXOPENID,
                COMM_USER.PETNAME,COMM_USER.BALANCE,COMM_USER.MILEAGE,COMM_USER.CREATETIME,COMM_USER.ACTIVEDATE,
                COMM_USER.ACTIVETIME,COMM_USER.LASTTIME,COMM_USER.BIRTHDAY,COMM_USER.PASSWORD,COMM_USER.ICON,
                COMM_USER.SEX,COMM_USER.CITY,COMM_USER.CHANNEL,COMM_USER.ICONSAVETYPE,COMM_USER.OIL,COMM_USER.STATUS,
                COMM_USER.ISAVALIABLE,COMM_USER.TOKEN3RD,COMM_USER.TYPE3RD,COMM_USER.PHONEID,COMM_USER.PRODUCT_ID,COMM_USER.REMARK);
        eq(COMM_USER.ISAVALIABLE,"1");
        eq(COMM_USER.STATUS,"1");
        return search(pageable,new CommuserInfoReponseMapper());
    }


    private final class CommuserInfoReponseMapper implements RecordMapper<Record,CommuserInfoSearchResponse>{

        @Override
        public CommuserInfoSearchResponse map(Record record) {
            CommuserInfoSearchResponse response = new CommuserInfoSearchResponse();
            response.setId(record.get(COMM_USER.ID));
            response.setActivedate(record.get(COMM_USER.ACTIVEDATE));
            response.setCreatetimes(record.get(COMM_USER.CREATETIME));
            response.setBirthday(record.get(COMM_USER.BIRTHDAY));
            response.setLasttime(record.get(COMM_USER.LASTTIME));
            response.setMobnum(record.get(COMM_USER.MOBNUM));
            response.setRemark(record.get(COMM_USER.REMARK));
            response.setPhoneid(record.get(COMM_USER.PHONEID));
            response.setActivetime(record.get(COMM_USER.ACTIVETIME));
            response.setBalance(record.get(COMM_USER.BALANCE));
            response.setStatus(record.get(COMM_USER.STATUS));
            response.setIsavaliable(record.get(COMM_USER.ISAVALIABLE));
            response.setOil(record.get(COMM_USER.OIL));
            response.setWechat(record.get(COMM_USER.WECHAT));
            response.setWeibo(record.get(COMM_USER.WEIBO));
            response.setWxopenid(record.get(COMM_USER.WXOPENID));
            response.setPassword(record.get(COMM_USER.PASSWORD));
            response.setChannel(record.get(COMM_USER.CHANNEL));
            response.setIcon(record.get(COMM_USER.ICON));
            response.setIconsavetype(record.get(COMM_USER.ICONSAVETYPE));
            response.setProduct_id(record.get(COMM_USER.PRODUCT_ID));
            response.setQq(record.get(COMM_USER.QQ));
            response.setSex(record.get(COMM_USER.SEX));
            response.setToken3rd(record.get(COMM_USER.TOKEN3RD));
            response.setType3rd(record.get(COMM_USER.TYPE3RD));
            response.setXiaomi(record.get(COMM_USER.XIAOMI));
            response.setPetname(record.get(COMM_USER.PETNAME));
            response.setCity(record.get(COMM_USER.CITY));
            return response;
        }
    }
    @Override
    protected TableImpl getTable() {
        return COMM_USER;
    }

    @Override
    public List<CommuserInfo> findCommuserBymobnum(CommuserInfosearchRequest request) {
        fields(COMM_USER.ID,
                COMM_USER.MOBNUM,
                COMM_USER.WEIBO,COMM_USER.QQ,COMM_USER.WECHAT,COMM_USER.XIAOMI,COMM_USER.WXOPENID,
                COMM_USER.PETNAME,COMM_USER.BALANCE,COMM_USER.MILEAGE,COMM_USER.CREATETIME,COMM_USER.ACTIVEDATE,
                COMM_USER.ACTIVETIME,COMM_USER.LASTTIME,COMM_USER.BIRTHDAY,COMM_USER.PASSWORD,COMM_USER.ICON,
                COMM_USER.SEX,COMM_USER.CITY,COMM_USER.CHANNEL,COMM_USER.ICONSAVETYPE,COMM_USER.OIL,COMM_USER.STATUS,
                COMM_USER.ISAVALIABLE,COMM_USER.TOKEN3RD,COMM_USER.TYPE3RD,COMM_USER.PHONEID,COMM_USER.PRODUCT_ID,COMM_USER.REMARK);
        eq(COMM_USER.ISAVALIABLE,"1");
        eq(COMM_USER.STATUS,"1");
        eq(COMM_USER.MOBNUM,request.getMobnum());
        return search(CommuserInfo.class);
    }

    private final class CommuserInfoMapper implements RecordMapper<Record,CommuserInfo>{
        @Override
        public CommuserInfo map(Record record) {
            CommuserInfo response = new CommuserInfo();
            response.setId(record.get(COMM_USER.ID));
            response.setActivedate(record.get(COMM_USER.ACTIVEDATE));
            response.setCreatetimes(record.get(COMM_USER.CREATETIME));
            response.setBirthday(record.get(COMM_USER.BIRTHDAY));
            response.setLasttime(record.get(COMM_USER.LASTTIME));
            response.setMobnum(record.get(COMM_USER.MOBNUM));
            response.setRemark(record.get(COMM_USER.REMARK));
            response.setPhoneid(record.get(COMM_USER.PHONEID));
            response.setActivetime(record.get(COMM_USER.ACTIVETIME));
            response.setBalance(record.get(COMM_USER.BALANCE));
            response.setStatus(record.get(COMM_USER.STATUS));
            response.setIsavaliable(record.get(COMM_USER.ISAVALIABLE));
            response.setOil(record.get(COMM_USER.OIL));
            response.setWechat(record.get(COMM_USER.WECHAT));
            response.setWeibo(record.get(COMM_USER.WEIBO));
            response.setWxopenid(record.get(COMM_USER.WXOPENID));
            response.setPassword(record.get(COMM_USER.PASSWORD));
            response.setChannel(record.get(COMM_USER.CHANNEL));
            response.setIcon(record.get(COMM_USER.ICON));
            response.setIconsavetype(record.get(COMM_USER.ICONSAVETYPE));
            response.setProduct_id(record.get(COMM_USER.PRODUCT_ID));
            response.setQq(record.get(COMM_USER.QQ));
            response.setSex(record.get(COMM_USER.SEX));
            response.setToken3rd(record.get(COMM_USER.TOKEN3RD));
            response.setType3rd(record.get(COMM_USER.TYPE3RD));
            response.setXiaomi(record.get(COMM_USER.XIAOMI));
            response.setPetname(record.get(COMM_USER.PETNAME));
            response.setCity(record.get(COMM_USER.CITY));
            return response;
        }


    }
}
